/**
 * @param {View|EmbeddedView} shell
 * @constructor
 * @extends BaseMediator
 */
function CaptionMediator(shell)
{
	"use strict";

	this.init(shell, '#caption', true);

	/** @type {CaptionMediator} */
	var _this = this;
	/** @type {String} */
	var _previousSetID = null;
	/** @type {jQuery} */
	var _scrollbar = _this.content.tinyscrollbar();
	var $scrollbar = _this.content.find('.scrollbar');
	var $viewport = _this.content.find('.viewport');
	var $description = _this.content.find('.description');
	var $dateTaken = _this.content.find('.date-taken');

	/**
	 * Update title and caption if given, otherwise hide caption
	 * @return {CaptionMediator}
	 */
	this.update = function()
	{
		updateSet();

		if (Format.isEmpty(Session.photo.title))
		{
			_this.bar.html(Session.set.title).addClass('default');
		}
		else
		{
			_this.bar.html(Session.photo.title).removeClass('default');
		}

		_this.bar.html(Format.isEmpty(Session.photo.title) ? Session.set.title : Session.photo.title);

		if (Format.isEmpty(Session.photo.description))
		{
			$scrollbar.hide();
			$description.hide();
			$viewport.animate({ height: 0 }, Enum.duration.captionResize);
		}
		else
		{
			$description.html(Session.photo.description).show();
			window.setTimeout(fit, 200);
		}
		$dateTaken.html(Format.date(Session.photo.dateTaken));

		return _this;
	};

	/**
	 * Update scrollbar after content is rendered
	 */
	function fit()
	{
		var h = $description.outerHeight(true);

		if (h > Enum.caption.maxHeight)
		{
			$description.find('sup')
				.css('cursor', 'help')
				.click(function() {	_scrollbar.tinyscrollbar_update('bottom'); });

			$viewport.animate({ height: Enum.caption.maxHeight }, Enum.duration.captionResize, function()
			{
				$scrollbar.show();
				_scrollbar.tinyscrollbar_update();
			});
		}
		else
		{
			$scrollbar.hide();
			$viewport.animate({ height: h }, Enum.duration.captionResize);
		}
	}

	/**
	 * @return {CaptionMediator}
	 */
	this.show = function()
	{
		updateSet();
		return _this.base.show.call(_this);
	};

	function updateSet()
	{
		if (!!Session.set && Session.set.id != _previousSetID)
		{
			_this.bar.html(Session.set.title);
			_previousSetID = Session.set.id;
		}
	}

}
CaptionMediator.inherit(BaseMediator);